﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UIShare;
using DevExpress.XtraGrid.Views.Grid;
using EFong.Contract;
using EFong.View.Proxy;

namespace EFong.View.Project
{
    public partial class Frm_ProjectEdit : BasicForm
    {
        private Proxy.MasterServiceProxy _masterProxy {get{ return Proxy.MasterServiceProxy.GetInstance();}}

        public Frm_ProjectEdit()
        {
            InitializeComponent();

            this.gv_SubProjects.OptionsView.NewItemRowPosition = NewItemRowPosition.Top;
        }

        protected override void InitFields()
        {
        }

        protected override void Events()
        {
            this.gv_SubProjects.InitNewRow += (s, e) =>
            {
                SubProject newSubProject = this.gv_SubProjects.GetRow(e.RowHandle) as SubProject;

                if (newSubProject != null)
                    newSubProject.Effective = true;
            };

            this.gv_SubProjects.CustomDrawCell += (s, e) =>
                {
                    this.gv_SubProjects.CustomDrawCell<SubProject>(e,
                        (subPrjt) =>
                        {
                            return !subPrjt.Effective;
                        },
                        () =>
                        {
                            e.Appearance.ForeColor = Color.Red;
                        });
                };
        }

        protected override void LoadDatas()
        {
            //this.cmb_ReportStyle.Properties.BindingData(ReportStyle.GetDatas());
        }

        private void bar_Main_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            switch (e.Item.Name)
            {
                case "bbt_Save":
                    this.save();
                    break;
                case "bbt_Close":
                    this.Close();
                    break;
                default:
                    break;
            }
        }

        private void save()
        {
            this.gv_SubProjects.PostEditor();

            Contract.Project project = getProject();

            UIOperManager.SaveData(
                () =>
                {
                    if (string.IsNullOrEmpty(project.Name))
                    {
                        MsgBox.Info("请输入项目名称.");
                        return false;
                    }

                    return true;
                },
                () =>
                {
                    updateToCurPrjt(project);
                    return _masterProxy.UpdateProject(CurPrjt).IsSuccess;
                },
                MsgResource.Save_Complete,
                () =>
                {
                    DialogResult = DialogResult.OK;
                    this.Close();
                }
                );
        }

        // current project
        private Contract.Project _curPrjt;
        public Contract.Project CurPrjt
        {
            get { return _curPrjt; }
            set
            {
                _curPrjt = value;
                onCurPrjtChanged();
            }
        }

        private void onCurPrjtChanged()
        {
            if (CurPrjt == null)
            {
                // clear project datas
            }
            else
            {
                // set project datas
                //this.cmb_ReportStyle.SetValue(CurPrjt.ReportStyle,"Value");
                this.txt_Name.SetText(CurPrjt.Name);
                //this.txt_Name.Enabled = CurPrjt.Editable;
                this.ckb_Effective.SetValue(CurPrjt.Effective);
                this.bds_SubProject.BindingData(CurPrjt.SubProjectList);

            }
        }

        private Contract.Project getProject()
        {
            Contract.Project project = new Contract.Project
            {
                Name = this.txt_Name.GetText(),
                Effective = this.ckb_Effective.GetValue(),
                SubProjectList = this.bds_SubProject.DataSource as List<SubProject>
            };

            //object reportStyle = this.cmb_ReportStyle.GetValue("Value");
            //if (reportStyle != null)
            //    project.ReportStyle = (int)reportStyle;

            return project;
        }

        private void updateToCurPrjt(Contract.Project project)
        {
            CurPrjt.Name = project.Name;
            CurPrjt.ReportStyle = project.ReportStyle;
            CurPrjt.Effective = project.Effective;
            CurPrjt.SubProjectList = project.SubProjectList;
        }
    }

    class ReportStyle
    {
        public string Display { get; set; }
        public object Value { get; set; }

        public override string ToString()
        {
            return Display;
        }

        public static List<ReportStyle> GetDatas()
        {
            return new List<ReportStyle> 
            {
                new ReportStyle
                {
                    Display = "格式1",
                    Value = 1
                },
                new ReportStyle
                {
                    Display = "格式2",
                    Value = 2
                }
            };
        }
    }
}
